package com.alatus;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;

public class swingEngine extends JFrame {
//    屏幕分辨率
    private static int screen_W = 1024;
    private static int screen_H = 682;
    private static int half_screen_W = screen_W / 2;
    private static int half_screen_H = screen_H / 2;
//    记载当前已经绚烂的帧数
    private static int frameIndex;
//    希望达到的每帧之间的间隔时间
    private static int frameInterval = 33;
//    使用jPanel作为画板
    private static JPanel panel;
//    使用一个int数组来存储屏幕像素数值
    private static int[] screen;
//    图像缓冲区,提供了在内存中操作屏幕图像的方式
    private static BufferedImage screenBuffer;
//    刷新率,计算刷新率所用的辅助参数
    private static int framePerSecond;
    private static long lastDraw;
    private static double lastTime,thisTime;
//    cpu睡眠时间
    private static int sleepTime;
    public static void main(String[] args) {
        new swingEngine();
    }
    public swingEngine() {
//        弹出一个JPanel窗口, 设置窗口大小,并将它放置在屏幕中间
        setTitle("Alatus软光栅渲染");
        panel = (JPanel) this.getContentPane();
        panel.setPreferredSize(new Dimension(screen_W, screen_H));
        panel.setMinimumSize(new Dimension(screen_W, screen_H));
        panel.setLayout(null);
        setResizable(false);
        pack();
        setVisible(true);
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(dimension.width / 2 - this.getSize().width / 2, dimension.height / 2 - screen_H / 2);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        使用TypeIntRGB来创建BufferedImage,然后把屏幕的像素数组指向BufferedImage中的DataBuffer
//        这样通过改变屏幕的像素数组中的数据就可以在屏幕上渲染出图象
        screenBuffer = new BufferedImage(screen_W, screen_H, BufferedImage.TYPE_INT_RGB);
        DataBuffer dest = screenBuffer.getRaster().getDataBuffer();
        screen = ((DataBufferInt)dest).getData();
//        渲染
        while(true){
//        把图像发送到显存,这是唯一用到显卡的地方
            int r_skyBlue = 163, g_skyBlue = 216, b_skyBlue = 239;
            int r_orange = 255, g_orange = 128, b_orange = 0;
//            渲染为天蓝色
//            for (int i = 0; i < screen.length; i++) {
//                screen[i] = (r_skyBlue << 16) | (g_skyBlue << 8) | b_skyBlue;
//            }
//            颜色由天蓝色过渡到橙色
//            for (int i = 0; i < screen_W; i++) {
//                for (int j = 0; j < screen_H; j++) {
//                    float t1 = Math.abs((float) (half_screen_W - i) / (half_screen_W));
//                    float t2 = 1f - t1;
//                    int r = (int) (r_skyBlue * t1 + r_orange * t2);
//                    int g = (int) (g_skyBlue * t1 + g_orange * t2);
//                    int b = (int) (b_skyBlue * t1 + b_orange * t2);
//                    screen[i + j * screen_W] = (r << 16) | (g << 8) | b;
//                }
//            }
//            把屏幕渲染成天蓝色过度到橙色的动态画面
            for (int i = 0; i < screen_W; i++) {
                int p = (i + frameIndex * 8) % screen_W;
                for (int j = 0; j < screen_H; j++) {
                    float t1 = Math.abs((float) (half_screen_W - p) / (half_screen_W));
                    float t2 = 1f - t1;
                    int r = (int) (r_skyBlue * t1 + r_orange * t2);
                    int g = (int) (g_skyBlue * t1 + g_orange * t2);
                    int b = (int) (b_skyBlue * t1 + b_orange * t2);
                    screen[i + j * screen_W] = (r << 16) | (g << 8) | b;
                }
            }
//            loop每次运行,帧数+1
            frameIndex++;
//            计算当前的刷新率,并尽量让刷新率恒定不变
            if(frameIndex%30==0){
                double thisTime = System.currentTimeMillis();
                framePerSecond = (int) (1000 / ((thisTime - lastTime)/30));
                lastTime = thisTime;
            }
            sleepTime = 0;
            while(System.currentTimeMillis() - lastDraw < frameInterval){
                try{
                    Thread.sleep(1);
                    sleepTime++;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            lastDraw = System.currentTimeMillis();
//            显示当前刷新率
            Graphics2D e2 = (Graphics2D) screenBuffer.getGraphics();
            e2.setColor(Color.BLACK);
            e2.drawString("FPS:"+framePerSecond+"ThreadSleep:"+sleepTime,5,15);
            panel.getGraphics().drawImage(screenBuffer, 0, 0, this);
        }
    }
}
